/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SERVLET;

import Utils.User;
import Utils.dbManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alice
 */
//@WebServlet(name="LoginServlet", urlPatterns={"/LoginServlet"})
public class LoginServlet extends HttpServlet {
    private dbManager manager;

    @Override
    public void init() throws ServletException{
        this.manager= (dbManager)super.getServletContext().getAttribute("dbManager");
    }

 
    @Override    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String docType=
         "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n";    
out.println(docType+"<html>");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
out.println("<title>LOGIN Page</title>");
//out.println("<meta name=\"generator\" content=\"WYSIWYG Web Builder 8 - http://www.wysiwygwebbuilder.com\">");
out.println("<style type=\"text/css\">");
out.println("body{");
out.println("   background-color: #FFFFFF;");
out.println("   color: #000000;}");
out.println("</style>");
out.println("<style type=\"text/css\">");
out.println("a{");
out.println("   color: #C8D7EB;");
out.println("   outline: none;");
out.println("   text-decoration: underline;}");
out.println("a:visited{");
out.println("   color: #C8D7EB;}");
out.println("a:active{");
out.println("   color: #C8D7EB;}");
out.println("a:hover{");
out.println("   color: #376BAD;");
out.println("   text-decoration: underline;}");
out.println("</style>");
out.println("<style type=\"text/css\">");
out.println("#wb_Text1 {");
out.println("   background-color: transparent;");
out.println("   border: 0px #000000 solid;");
out.println("   padding: 0;}");
out.println("#wb_Text1 div{");
out.println("   text-align: left;}");
out.println("#wb_Text2 {");
out.println("   background-color: transparent;");
out.println("   border: 0px #000000 solid;");
out.println("   padding: 0;}");
out.println("#wb_Text2 div{");
out.println("   text-align: left;}");
out.println("#wb_Text3 {");
out.println("   background-color: transparent;");
out.println("   border: 0px #000000 solid;");
out.println("   padding: 0;}");
out.println("#wb_Text3 div{");
out.println("   text-align: left;}");
out.println("#Editbox1{");
out.println("   border: 1px #C0C0C0 solid;");
out.println("   background-color: #FFFFFF;");
out.println("   color :#000000;");
out.println("   font-family: Arial;");
out.println("   font-size: 13px;");
out.println("   text-align: left;");
out.println("   vertical-align: middle;}");
out.println("#Editbox2{");
out.println("   border: 1px #C0C0C0 solid;");
out.println("   background-color: #FFFFFF;");
out.println("   color :#000000;");
out.println("   font-family: Arial;");
out.println("   font-size: 13px;");
out.println("   text-align: left;");
out.println("   vertical-align: middle;}");
out.println("#wb_Text4{ ");
out.println("   background-color: transparent;");
out.println("   border: 0px #000000 solid;");
out.println("   padding: 0;}");
out.println("#wb_Text4 div{");
out.println("   text-align: left;}");
out.println("</style>");
out.println("</head>");
out.println("<body>");
out.println("<div id=\"wb_Text1\" style=\"position:absolute;left:357px;top:170px;width:606px;height:34px;z-index:0;\">");
out.println("<span style=\"color:#000000;font-family:Arial;font-size:29px;\"><strong>Autenticazione</strong></span></div>");

out.println("    <div>");out.println("<form method=\"post\" action=\"LoginServlet\">"); 

//out.println("<%");
//out.println("    if(request.getParameter(\"error\") != null){");
//out.println("                out.append(\"<div class=\'alert alert-error\'>\");");
//out.println("                out.append(request.getParameter(\"error\"));");
//out.println("                out.append(\"</div>\");}%>");
out.println("    </div>");
out.println("    <div>");
    //<label class="control-label" for="input01">Username</label>&nbsp;
out.println("<div id=\"wb_Text2\" style=\"position:absolute;left:36px;top:316px;width:120px;height:22px;z-index:1;\">");
out.println("<span style=\"color:#000000;font-family:Arial;font-size:19px;\">Username :</span></div>");

            //<input id="input01" class="input-large" type="text" name="user"/>
out.println("<input type=\"text\" id=\"Editbox1\" style=\"position:absolute;left:153px;top:313px;width:152px;height:22px;line-height:22px;z-index:3;\" name=\"user\" value=\"\">");

out.println("    </div>");
out.println("    <div>");
    //<label class="control-label" for="input02">Password</label>&nbsp;
out.println("<div id=\"wb_Text3\" style=\"position:absolute;left:36px;top:357px;width:99px;height:22px;z-index:2;\">");
out.println("<span style=\"color:#000000;font-family:Arial;font-size:19px;\">Password :</span></div>");

            //<input id="input02" class="input-large" type="password" name="password"/><br>
out.println("<input type=\"text\" id=\"Editbox2\" style=\"position:absolute;left:153px;top:353px;width:152px;height:22px;line-height:22px;z-index:4;\" name=\"passw\" value=\"\">");

out.println("   </div>");
out.println("    <div>");
    //<button class="btn btn-primary" type="submit" id="butt_login"  value="Login">Login</button>
out.println("<button id=\"AdvancedButton1\" type=\"submit\" name=\"\" value=\"Login\" style=\"position:absolute;left:432px;top:512px;width:94px;height:40px;z-index:6;\"><div style=\"text-align:center\">\"");
out.println("<span style=\"color:#000000;font-family:Arial;font-size:17px\">LOGIN</span></div></button>");
out.println("    </div>");

out.println("</form>");
out.println("<div id=\"wb_Text4\" style=\"position:absolute;left:216px;top:64px;width:518px;height:45px;z-index:7;\">");
out.println("<span style=\"color:#0000FF;font-family:'Comic Sans MS';font-size:32px;\"><em><u>MERCATO AGRO-ALIMENTARE</u></em></span></div>");
out.println("</body>");
out.println("</html>");
    }
 
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
    
 String username= req.getParameter("username");
 String password= req.getParameter("password");
 User user;
 try{
     user= manager.authenticate(username,password);     
 } catch (SQLException ex){
     throw new ServletException(ex);
 }
 if (user==null){
     req.setAttribute("message", "Username/password non esistente!");
     RequestDispatcher rd= req.getRequestDispatcher("/ErrorLoginServlet");
     rd.forward(req,resp);
 }else{
     HttpSession session= req.getSession(true);
     session.setAttribute("user", user);
     resp.sendRedirect(req.getContextPath()+"/LandingBuyerServlet");
 }
}}


    
    
    
    
    
    
    
    
    
    
    
    /**protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
    HttpSession session= request.getSession();
    String password= request.getParameter("password");
    String username= request.getParameter("username");
    
    User x=
    switch(x.getTypes())
    {
        case 1: //BUYER
            session.getAttribute("username", x);
            response.sendRedirect("LandingBuyerServlet");
            break;
            
        case 2: //SELLER
            session.getAttribute("username", x);
            response.sendRedirect("LandingSellerServlet");
            break;
            
        default : //ERRORE
            session.invalidate();
            response.sendRedirect("ErrorLoginServlet");
    }
    
}}*/

   